package com.coolgor.coldot.schedule.Friday;

import com.coolgor.coldot.dto.stock.MarketMiniteExecution;
import com.coolgor.coldot.dto.stock.StockFollowerExecution;
import com.coolgor.coldot.dto.stock.StockInfoExecution;
import com.coolgor.coldot.dto.stock.StockSpecialDayExecution;
import com.coolgor.coldot.dto.wechat.qyh.QYHMessageExecution;
import com.coolgor.coldot.entity.stock.MarketMinite;
import com.coolgor.coldot.entity.stock.StockFollower;
import com.coolgor.coldot.entity.stock.StockInfo;
import com.coolgor.coldot.entity.stock.StockSpecialDay;
import com.coolgor.coldot.enums.stock.MarketMiniteOperationStateEnum;
import com.coolgor.coldot.enums.stock.StockInfoOperationStateEnum;
import com.coolgor.coldot.enums.stock.StockSpecialDayOperationStateEnum;
import com.coolgor.coldot.service.stock.MarketMiniteService;
import com.coolgor.coldot.service.stock.StockFollowerService;
import com.coolgor.coldot.service.stock.StockInfoService;
import com.coolgor.coldot.service.stock.StockSpecialDayService;
import com.coolgor.coldot.service.wechat.gzh.GZHMessageService;
import com.coolgor.coldot.service.wechat.qyh.QYHMessageService;
import com.coolgor.xutil.Common.PubFunc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class StockService {

    @Autowired
    QYHMessageService qyhMessageService;

    @Autowired
    GZHMessageService gzhMessageService;

    @Autowired
    StockInfoService stockInfoService;

    @Autowired
    StockFollowerService stockFollowerService;

    @Autowired
    StockSpecialDayService stockSpecialDayService;

    @Autowired
    MarketMiniteService marketMiniteService;


    public List<String> getStockCodeList(){
        StockInfo condition = new StockInfo();
        condition.setFlag(1);
        StockInfoExecution exe = stockInfoService.getList(condition, 1, 999);
        if (exe.getState() == StockInfoOperationStateEnum.SUCCESS.getState()) {
            List<String> list = new ArrayList<String>();
            for (StockInfo stockInfo : exe.getStockInfoList()) {
                list.add(stockInfo.getCode());
            }
            if (list.size()>0) {
                return list;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public List<String> getEmptyNameCodeList() {
        StockInfo condition = new StockInfo();
        condition.setFlag(1);
        StockInfoExecution exe = stockInfoService.getList(condition, 1, 999);
        if (exe.getState() == StockInfoOperationStateEnum.SUCCESS.getState()) {
            List<String> list = new ArrayList<String>();
            for (StockInfo stockInfo : exe.getStockInfoList()) {
                if (null==stockInfo.getName() || "".equals(stockInfo.getName())) {
                    list.add(stockInfo.getCode());
                }
            }
            if (list.size()>0) {
                return list;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public boolean isWorkday(Date date){
        StockSpecialDay dayCondition = new StockSpecialDay();
        dayCondition.setBeginDate(date);
        dayCondition.setFlag(1);
        dayCondition.setCategory("whitelist");
        StockSpecialDayExecution exe = stockSpecialDayService.getList(dayCondition,1,99);
        if (exe.getState()== StockSpecialDayOperationStateEnum.SUCCESS.getState()
                && exe.getCount()==0 ) {
            return true;
        }

        String weekday = PubFunc.getWeekOfDate(date);
        if (null==weekday) return false;
        switch (weekday) {
            case "周一","周二","周三","周四","周五":
                dayCondition.setCategory("blacklist");
                exe = stockSpecialDayService.getList(dayCondition,1,99);
                if (exe.getState()== StockSpecialDayOperationStateEnum.SUCCESS.getState()
                        && exe.getCount()==0 ){
                    return false;
                } else {
                    return true;
                }
            case "周六","周日" :
                // 上面执行过白名单过滤了，这里就不需要了
                return false;
        }
        return false;
    }

    public void trackStock() {
        StockFollower stockFollower = new StockFollower();
        stockFollower.setFlag(1);
        StockFollowerExecution stockFollowerExecution = stockFollowerService.getList(stockFollower,1,999);
        if (stockFollowerExecution.getState()== StockSpecialDayOperationStateEnum.SUCCESS.getState()
            && stockFollowerExecution.getCount()>0 ){
            Date nowDate = new Date();
            for (StockFollower follower : stockFollowerExecution.getStockFollowerList()) {
                if ( null!=follower.getLastReportTime()
                        && (nowDate.getTime()-follower.getLastReportTime().getTime())<(60*60*1000)) continue;
                MarketMinite marketMinite = new MarketMinite();
                marketMinite.setCode(follower.getCode());
                MarketMiniteExecution marketMiniteExecution = marketMiniteService.getList(marketMinite,1,3);
                if (marketMiniteExecution.getState()== MarketMiniteOperationStateEnum.SUCCESS.getState()) {
                    List<MarketMinite> list = marketMiniteExecution.getMarketMiniteList();
                    // 更新follower表格中的股票名称
                    if (null!=list.get(0).getName() && !"".equals(list.get(0).getName())
                        && !list.get(0).getName().equals(follower.getName())){
                        follower.setName(list.get(0).getName());
                        stockFollowerService.modify(follower);
                    }
                    // 执行规则判断操作
                    switch (follower.getMeasure()) {
                        case ">":
                            if (Float.valueOf(list.get(0).getCurrentPrice()) > Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(1).getCurrentPrice()) < Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(2).getCurrentPrice()) < Float.valueOf(follower.getQuota())) {
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
//                                qyhMessageService.sendText(9L, "@all", null, null, msg, false);
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);

                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                        case ">=":
                            if (Float.valueOf(list.get(0).getCurrentPrice()) >= Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(1).getCurrentPrice()) < Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(2).getCurrentPrice()) < Float.valueOf(follower.getQuota())) {
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);
                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                        case "=":
                            if (Float.valueOf(list.get(0).getCurrentPrice()) == Float.valueOf(follower.getQuota())) {
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);
                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                        case "<":
                            if (Float.valueOf(list.get(0).getCurrentPrice()) < Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(1).getCurrentPrice()) > Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(2).getCurrentPrice()) > Float.valueOf(follower.getQuota())) {
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);
                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                        case "<=":
                            if (Float.valueOf(list.get(0).getCurrentPrice()) <= Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(1).getCurrentPrice()) > Float.valueOf(follower.getQuota())
                                && Float.valueOf(list.get(2).getCurrentPrice()) > Float.valueOf(follower.getQuota())) {
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);
                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                        case "near":
                            if (Math.abs(Float.valueOf(list.get(0).getCurrentPrice())-Float.valueOf(follower.getQuota())) < (Float.valueOf(follower.getQuota())*0.005)) {
                                // 更新最后播报时间
                                follower.setLastReportTime(new Date());
                                stockFollowerService.modify(follower);
                                // 发送信息
                                String msg = follower.getCode()+"|"+follower.getName()+"\\n"+follower.getContext();
//                                qyhMessageService.sendText(9L, "@all", null, null, msg, false);
                                System.out.println(follower.getFollower());
                                qyhMessageService.sendText(9L, follower.getFollower(), null, null, msg, false);
                                qyhMessageService.sendText(10L, follower.getFollower(), null, null, msg, false);
                                gzhMessageService.sendTemplateMessage("wx4302c9ce605087e1",
                                        "工作任务提醒",
                                        "oQj0it9tozdjZfwUKsZjkp4WLQJ8",
                                        follower.getCode()+"-"+follower.getName(),
                                        follower.getContext(),
                                        null,
                                        "https://finance.sina.com.cn/realstock/company/"+follower.getCode()+"/nc.shtml");
                            }
                            break;
                    }
                }
            }
        }

    }

    

}